
=begin
Copyright (C) 2014 Enrique Rando

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

http://www.gnu.org/licenses/gpl-2.0.html
=end



=begin

THIS CLASS IS MEANT FOR EDUCATIONAL PURPOSES ONLY

IF YOU NEED TO USE THIS SEARCH ENGINE, PLEASE IMPLEMENT IT IN A RUBY CLASS AS INDICATED IN THE DOCUMENTATION USING THE APPROPIATE APIs

=end


require File.expand_path(File.dirname(__FILE__) + '/../Search_engine.rb')

class Yandex_web < Search_engine
	# Object creation
	# Parameters
	#		max_requests	maximum number of requests
	#		proxy			Proxy name or address. Use nil for no proxy.
	#						By default (:ENV), Ruby 2.1 uses HTTP_PROXY environment variable to set the proxy
	#		proxy_port		Proxy port
	#		proxy_user		Proxy username, if required
	#		proxy_password	Proxy password, if required.
	def initialize(max_requests=3, proxy=:ENV, proxy_port=nil, proxy_user=nil, proxy_password=nil)
		# Regexps for data recognition
		regexps = {
			:begin_result =>/<div class="serp-item__wrap island clearfix">/,
			:end_results => /<div class="content__right/,
			:document_type => /<img class="serp-item__mime-icon"[^>]+alt=["'](?<data>.*?)["']>/,
			:title => /<a class="b-link serp-item__title-link[^>]+?>(?<data>.*?)<\/a>/,
			:url => /<a class="b-link serp-item__title-link[^>]+?href="(?<data>[^"]*?)"/,
			:summary => /<div class="serp-item__text">(?<data>.*?)<\/div>/,
			:cache => /.*href="(?<data>.*?)">Cached page/
		}

		# Mechanisms used for result paging
		paging = {
			:parameter => "p", 
			:parameter_method => :get, 
			:size_parameter => 'tpht',
			:size_parameter_method => :get,
			:size_parameter_value => '', 
			:size => 10
		}

		# Create the Search Engine
		super(
			:https, "www.yandex.com", :default, "/yandsearch", "text",
			regexps, paging,
			{}, "", {},
			"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0",
			"Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)",
			max_requests
		)
		
		# Establish connection
		connect_through(proxy, proxy_port, proxy_user, proxy_password)
	end

private

	# Creates the cache info
	def get_cache(result)
		data = super(result)
		
		if data.nil?
			return nil
		else
			return data.gsub("&amp;", "&").force_encoding("ascii-8bit")
		end
	end
	
	# Creates the URLs needed to translate a result
	def get_translate(result)
		url = get_url(result)
		url = url.sub(/^.*?:\/\//, "")
		return [{
			:url => "https://z5h64q92x9.net/tr-url/auto-en-ru.en/#{url}".force_encoding("ascii-8bit"),
			:referer => nil
		}]
	end
	
	def compute_page_start(x)
		return x - 1
	end
end
